﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ADL
{
    public class ADL_gift
    {
        /// <summary>
        /// 增
        /// </summary>
        /// <param name="tb"></param>
        /// <returns></returns>
        public string insert(_gift tb)
        {
            if (tb._gift_id != null)
            {
                CdioDBDataContext da = new CdioDBDataContext();
                da._gift.InsertOnSubmit(tb);
                da.SubmitChanges();
                string id = "" + tb._gift_id;
                return id;
            }
            return null;
        }

        /// <summary>
        /// 删
        /// </summary>
        /// <param name="tb"></param>
        /// <returns></returns>
        public bool delete(_gift tb)
        {
            CdioDBDataContext da = new CdioDBDataContext();
            var p = da._gift.Where(item => item._gift_id == tb._gift_id);
            if (p.Any())
            {
                da._gift.DeleteOnSubmit(p.ToList()[0]);
                da.SubmitChanges();
                return true;
            }
            return false;
        }

        /// <summary>
        /// 改
        /// </summary>
        /// <param name="ct"></param>
        /// <returns></returns>
        public bool update(_gift tb)
        {
            CdioDBDataContext da = new CdioDBDataContext();
            var p = da._gift.Where(item => item._gift_id == tb._gift_id);
            if (p.Any())
            {
                var var_table = p.ToList()[0];
                var_table._gift_id = tb._gift_id;
                var_table._gift_type = tb._gift_type;
                var_table._gift_name = tb._gift_name;
                var_table._gift_credit = tb._gift_credit;
                var_table._gift_src = tb._gift_src;
                var_table._gift_account = tb._gift_account;
                var_table._gift_state = tb._gift_state;
                var_table._gift_rangetime = tb._gift_rangetime;
                var_table._gift_sorttime = tb._gift_sorttime;
                var_table._gift_descript = tb._gift_descript;
                da.SubmitChanges();
                return true;
            }
            return false;
        }

        /// <summary>
        /// 查
        /// </summary>
        /// <param name="ct"></param>
        /// <param name="refrenceskey">根据那个键值查询</param>
        /// <returns></returns>
        public List<_gift> search(_gift tb, string refrenceskey)
        {
            CdioDBDataContext da = new CdioDBDataContext();
            switch (refrenceskey)
            {
                case "_user_id":
                    if (tb._gift_id != null)
                    {
                        var p = da._gift.Where(item => item._gift_id == tb._gift_id);
                        if (p.Any())
                        {
                            return p.ToList();
                        }
                    }
                    break;
                case "*":
                    var p1 = da._gift;
                    if (p1.Any())
                    {
                        return p1.ToList();
                    }
                    break;
            }
            return null;
        }
    }
}
